//
//  MSCheckDetailedController.m
//  觅尚
//
//  Created by No.8 on 2019/3/26.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSKaoqingController.h"
#import "MSDepartmentAttendanceController.h"
#import "MSMyMonthController.h"
#import "MSAttendanceCardController.h"
#import "AppDelegate.h"

@interface MSKaoqingController ()<UITableViewDelegate,UITableViewDataSource>
{
    CustomNavigationBar *naviagtionBar;
}

/** 选择按钮*/
@property(nonatomic,strong)UIButton *selectedButton;


/** 底部所有的标签 */
@property (nonatomic, strong) UIView  * titlesView;



/** 底部所有的内容 */
@property (nonatomic, strong) UIScrollView  * contenView;

/** 遮盖层*/
@property(nonatomic,strong)UIView *backView;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView;

/** <#name#>*/
@property(nonatomic,strong)NSString *updateStatus;



@property(nonatomic,strong)NSString *updateDescription;

/** 遮盖层*/
@property(nonatomic,strong)UIView *backView1;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView1;

/** <#name#>*/
@property(nonatomic,strong)NSString *versionStatus;

@end

@implementation MSKaoqingController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = backgroundcolor;
    
    [self CreateNavigationBar];
    
    
    [self CreatView];
    
    //初始化子控制器
    [self setupChildVces];
    
    
    [self setupScrollView];
    
    [self shareAppVersionAlert];
    
}

- (void)shareAppVersionAlert {
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    
    NSString *appVersion = [NSString stringWithFormat:@"v%@",infoDict[@"CFBundleShortVersionString"]];
    //
    NSDictionary * params = @{@"clientCategory":@3,@"currentVersionNumber":appVersion};
    
    WYCLog(@"--------%@",params);
    
    [HTTPRequest POST:MScheckUpdate params:params succeed:^(id responseObject) {
        WYCLog(@"--------%@",responseObject);
        if (CodeStr) {
            NSDictionary * dict = responseObject[@"data"];
            if ([dict[@"newVersion"] isEqual:@"-1"]) {
                
            }else{
                NSString * updateStatus = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"newVersion"][@"updateStatus"]];
                self.updateStatus = updateStatus;
                self.versionStatus = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"currentVersion"][@"versionStatus"]];
                self.updateDescription = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"newVersion"][@"updateDescription"]];
                [self GupdateStatus];
            }
        }else{
            
            
        }
        
    } failure:^(NSError *error) {
        WYCLog(@"--------%@",error);
        [MBManager showBriefAlert:Failed];
    }];
    
    
    
}











- (void)GupdateStatus{
    
    if ([self.versionStatus intValue] != 1) {
    if ([self.updateStatus isEqualToString:@"1"]) {
        UIView * view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [[UIApplication sharedApplication].keyWindow  addSubview:view];
        self.backView1 = view;
        
        
        UIView * showView = [[UIView alloc] init];
        showView.frame = CGRectMake(200 * px, ScreenHeight / 2 , ScreenWidth - 400 * px, ScreenHeight / 5);
        showView.backgroundColor = [UIColor whiteColor];
        showView.layer.cornerRadius = 20 * px;
        [view addSubview:showView];
        self.showView1 = showView;
        
        
        UIButton * choseButton = [[UIButton alloc] init];
        choseButton.frame = CGRectMake(showView.width - 90 * px, 20 * px, 60 * px, 60 * px);
        [choseButton setImage:[UIImage imageNamed:@"pop_dis"] forState:UIControlStateNormal];
        [choseButton addTarget:self action:@selector(choseButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:choseButton];
        
        /** title*/
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 * px, showView.width, 60 * px)];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.text = @"更新最新版本";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:SYRealValue(35 / 2)];
        [showView addSubview:titleLabel];
        
        
        /** title*/
        UILabel * Label = [[UILabel alloc] initWithFrame:CGRectMake(10 * px, CGRectGetMaxY(titleLabel.frame) + 20 * px, showView.width, 60 * px)];
        Label.textColor = [UIColor blackColor];
        Label.text = [NSString stringWithFormat:@"%@",self.updateDescription];
        Label.textAlignment = NSTextAlignmentLeft;
        Label.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [showView addSubview:Label];
        
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, showView.height / 3 * 2, showView.width, 1)];
        lineView.backgroundColor = WYCRGBColor(106, 186, 246);
        [showView addSubview:lineView];
        /** 确定*/
        UIButton * quuedingButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px, CGRectGetMaxY(lineView.frame) + 40 * px, showView.width / 2 - 200 * px, 60 * px)];
        [quuedingButton setTitle:@"确定" forState:UIControlStateNormal];
        quuedingButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [quuedingButton  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [quuedingButton  addTarget:self action:@selector(quuedingButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:quuedingButton];
        
        
        /** 确定*/
        UIButton * quxiaoButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px + showView.width / 2, quuedingButton.y, quuedingButton.width,quuedingButton.height)];
        [quxiaoButton setTitle:@"取消" forState:UIControlStateNormal];
        quxiaoButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [quxiaoButton  setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [quxiaoButton  addTarget:self action:@selector(quxiaoButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:quxiaoButton];
    }else if ([self.updateStatus isEqualToString:@"2"]){
        UIView * view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [[UIApplication sharedApplication].keyWindow  addSubview:view];
        self.backView = view;
        
        
        UIView * showView = [[UIView alloc] init];
        showView.frame = CGRectMake(200 * px, ScreenHeight / 2 , ScreenWidth - 400 * px, ScreenHeight / 5);
        showView.backgroundColor = [UIColor whiteColor];
        showView.layer.cornerRadius = 20 * px;
        [view addSubview:showView];
        self.showView = showView;
        
        
        
        
        /** title*/
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 * px, showView.width, 60 * px)];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.text = @"更新最新版本";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:SYRealValue(35 / 2)];
        [showView addSubview:titleLabel];
        
        
        /** title*/
        UILabel * Label = [[UILabel alloc] initWithFrame:CGRectMake(10 * px, CGRectGetMaxY(titleLabel.frame) + 20 * px, showView.width, 60 * px)];
        Label.textColor = [UIColor blackColor];
        Label.text = [NSString stringWithFormat:@"%@",self.updateDescription];
        Label.textAlignment = NSTextAlignmentLeft;
        Label.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [showView addSubview:Label];
        
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, showView.height / 3 * 2, showView.width, 1)];
        lineView.backgroundColor = WYCRGBColor(106, 186, 246);
        [showView addSubview:lineView];
        /** 确定*/
        UIButton * quuedingButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px, CGRectGetMaxY(lineView.frame) + 40 * px, showView.width / 2 - 200 * px, 60 * px)];
        [quuedingButton setTitle:@"确定" forState:UIControlStateNormal];
        quuedingButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [quuedingButton  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [quuedingButton  addTarget:self action:@selector(quuedingButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:quuedingButton];
        
        
        /** 确定*/
        UIButton * quxiaoButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px + showView.width / 2, quuedingButton.y, quuedingButton.width,quuedingButton.height)];
        [quxiaoButton setTitle:@"取消" forState:UIControlStateNormal];
        quxiaoButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [quxiaoButton  setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [quxiaoButton  addTarget:self action:@selector(QianzhiquxiaoButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:quxiaoButton];
        
        
        
    }else{
        
        
    }
    }else{
        
    }
}

- (void)quxiaoButtonClick{
    
    [self choseButtonClick];
}
- (void)quuedingButtonClick{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1458685882"]];
    
}
- (void)QianzhiquxiaoButtonClick{
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
}
- (void)choseButtonClick{
    
    [self.backView1 removeFromSuperview];
}

/**
 *底部的scrollview
 */
- (void)setupScrollView
{
    //不自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView * contenView = [[UIScrollView alloc]init];
    contenView.frame = self.view.bounds;
    contenView.delegate = self;
    contenView.pagingEnabled = YES;
    
    [self.view insertSubview:contenView atIndex:0];
    contenView.contentSize = CGSizeMake(contenView.width * self.childViewControllers.count,0);
    
    self.contenView = contenView;
    //
    //    //添加第一个控制器view
    [self scrollViewDidEndScrollingAnimation:contenView];
}

- (void)setupChildVces{
    MSDepartmentAttendanceController * DayVC = [[MSDepartmentAttendanceController alloc] init];
    [self addChildViewController:DayVC];
    
    MSMyMonthController * MonthVC = [[MSMyMonthController alloc] init];
    [self addChildViewController:MonthVC];
    
    
}
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}




- (void)CreatView{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = BackViewColor;
    titlesView.frame = CGRectMake(0, [self navBarBottom], ScreenWidth, 150 * px);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    
    NSArray * titleArray = @[@"日",@"月"];
    
    CGFloat ButtonWidth = ScreenWidth / titleArray.count;
    CGFloat ButtonHeight = titlesView.height;
    CGFloat ButtonY = 0;
    for (NSInteger i = 0; i < titleArray.count; i ++) {
        UIButton * Button = [[UIButton alloc] init];
        Button.tag = i;
        [Button layoutIfNeeded];//强制布局
        Button.x = ButtonWidth * i;
        Button.y = ButtonY;
        Button.width = ButtonWidth;
        Button.height = ButtonHeight;
        [Button setTitle:titleArray[i] forState:UIControlStateNormal];
        [Button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [Button setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [Button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [Button setBackgroundImage:[UIImage imageWithColor:BackViewColor] forState:UIControlStateNormal];
        [Button setBackgroundImage:[UIImage imageWithColor:FontColor] forState:UIControlStateDisabled];
        Button.titleLabel.font = [UIFont systemFontOfSize:28 / 2];
        //        [Button setBackgroundColor:BackViewColor];
        [titlesView addSubview:Button];
        // 默认点击了第一个按钮
        if (i == 0) {
            [self ButtonClick:Button];
            Button.enabled = NO;
            
            self.selectedButton = Button;
        }
    }
    
}
- (void)ButtonClick:(UIButton *)button{
    //修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    //滚动
    CGPoint offset = self.contenView.contentOffset;
    offset.x = button.tag * self.contenView.width;
    [self.contenView setContentOffset:offset animated:YES];
    
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    //当前的索引
    //长度NSInteger * interger;
    int index = scrollView.contentOffset.x / scrollView.width;
    
    //取出子控制器
    UIViewController * vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = [self navBarBottom] + self.titlesView.height;//tableview的高度默认为20
    vc.view.width = ScreenWidth;
    vc.view.height = scrollView.height;//tableview的底部
    //    //设置内边距
    //
    //    CGFloat bottom = self.tabBarController.tabBar.height;
    //    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
    //    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    //    //设置滚动条的内边距
    //    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //点击按钮
    int index = scrollView.contentOffset.x / scrollView.width;
    [self ButtonClick:self.titlesView.subviews[index]];
    
    
    // 添加子控制器的view
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"考勤统计";
    naviagtionBar.leftBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    [naviagtionBar.rightBtn setTitle:@"我的考勤" forState:UIControlStateNormal];
    [naviagtionBar.rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [naviagtionBar.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)rightBtnClick{
    WYCLogFunc;
    MSAttendanceCardController *AttendanceVC = [[MSAttendanceCardController alloc] init];
    [self.navigationController pushViewController:AttendanceVC animated:NO];
}

- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

@end
